package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessTechnology;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.TechnologyVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.TechnologyHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：农技智库
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：白彦东
 * 创建时间：2017/1/4 17:15
 * 修改备注：
 * ================================================
 */

public class TechnologyListActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;
    @Bind(R.id.technology_search)
    RelativeLayout technologySearch; // 搜索
    @Bind(R.id.technology_tv_ask)
    TextView technologyTvAsk; // 提问
    @Bind(R.id.technology_tv_answer)
    TextView technologyTvAnswer; // 回答

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;
    private String keyWords = "";

    public static final int REQUEST_CODE_SEARCH = 99;
    public static final int REQUEST_CODE_ANSWER = 88;

    private List<TechnologyVO> technologyVOList = new ArrayList<>();

    @Override
    public void setLayout() {
        setTitle("农技智库");
        setContentLayout(R.layout.activity_technology_list);
    }

    @Override
    public void initMember() {
        initRecyclerView();
    }

    private void initRecyclerView() {


        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.addItemDecoration(Utils.setSpaceDecoration(this, false, false, false));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new TechnologyHolder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
//                GotoUtil.gotoActivity(TechnologyListActivity.this, AnswerListActivity.class, "TechnologyVO", adapter.getItem(position));
                if (Utils.isLogin(TechnologyListActivity.this))
                    GotoUtil.gotoForResultActivity(TechnologyListActivity.this, AnswerListActivity.class, REQUEST_CODE_ANSWER, "TechnologyVO", adapter.getItem(position));
                else
                    Utils.notLoginGoto(TechnologyListActivity.this);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (msg.what) {
                    case MSG.MSG_TECHNOLOGY_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_TECHNOLOGY_LIST_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
//
        technologyVOList = FastJsonUtil.getListBean(object.toString(), "body", "answers", TechnologyVO.class);
        if (page == 1) { // 刷新
            adapter.clear();
        }

        if (technologyVOList != null) {
            if (technologyVOList.size() < limit) {
                adapter.stopMore();
            }
        }

        adapter.addAll(technologyVOList);
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessTechnology.getTechnologyList(this, keyWords, String.valueOf(page), String.valueOf(limit), mHandler);
    }

    @Override
    public void onLoadMore() {
            page++;
            BusinessTechnology.getTechnologyList(this, keyWords, String.valueOf(page), String.valueOf(limit), mHandler);
    }

    @OnClick({R.id.technology_search, R.id.technology_tv_ask, R.id.technology_tv_answer})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.technology_search: // 搜索
                GotoUtil.gotoForResultActivity(TechnologyListActivity.this, SearchTechnologyActivity.class, REQUEST_CODE_SEARCH);
                break;
            case R.id.technology_tv_ask: // 提问
                if (Utils.isLogin(this))
                    GotoUtil.gotoActivity(this, TechnologyAskActivity.class);
                else
                    Utils.notLoginGoto(this);
                break;
            case R.id.technology_tv_answer: // 回答
                if (Utils.isLogin(this))
                    GotoUtil.gotoActivity(this, TechnologyAnswerListActivity.class);
                else
                    Utils.notLoginGoto(this);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE_SEARCH: // 搜索
                    keyWords = data.getStringExtra("query");
                    onRefresh();
                    break;
                case REQUEST_CODE_ANSWER: // 回答列表
                    String questionId = data.getStringExtra("questionId");
                    for (int i = 0; i < technologyVOList.size(); i++) {
                        if (questionId.equals(technologyVOList.get(i).getQuestion().getId())) {
                            adapter.remove(technologyVOList.get(i));
                        }
                    }
                    break;
            }
        }
    }
}
